package com.example.handler;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.aspect.HttpAspect;
import com.example.bean.Result;
import com.example.enums.CodeEnum;
import com.example.exception.CommonException;
import com.example.utils.ResultUtil;

/**
 * @author edehhjn
 * 对抛出异常进行拦截统一封装处理
 */
@ControllerAdvice
public class ExceptionHandler {
	
	private final Logger logger = LoggerFactory.getLogger(ExceptionHandler.class);
	
	@org.springframework.web.bind.annotation.ExceptionHandler(value=Exception.class)
	@ResponseBody
	public Result handle(Exception e) {
		logger.error("error={}",e);
		if(e instanceof CommonException) {
			CommonException exception  = (CommonException)e;
			return ResultUtil.error(exception.getCode(),exception.getMessage());
		}else {//默认异常为100
			return ResultUtil.error(CodeEnum.ERROR_SYS.getCode(),e.getMessage());
		}
	}
}
